﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Purchase.aspx.cs" Inherits="Automobile.service.UI.Purchase" %>

<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,member-scalable=no" />
    <meta http-equiv="Cache-Control" content="no-siteapp" />
    <!--[if lt IE 9]>
<script type="text/javascript" src="lib/html5.js"></script>
<script type="text/javascript" src="lib/respond.min.js"></script>
<script type="text/javascript" src="lib/PIE_IE678.js"></script>
<![endif]-->
    <link href="css/H-ui.min.css" rel="stylesheet" type="text/css" />
    <link href="css/H-ui.admin.css" rel="stylesheet" type="text/css" />
    <link href="lib/Hui-iconfont/1.0.1/iconfont.css" rel="stylesheet" type="text/css" />
    <!--[if IE 6]>
<script type="text/javascript" src="http://lib.h-ui.net/DD_belatedPNG_0.0.8a-min.js" ></script>
<script>DD_belatedPNG.fix('*');</script>
<![endif]-->
    <title>采购</title>
</head>
<body>

    <nav class="breadcrumb"><i class="Hui-iconfont">&#xe67f;</i> 首页 <span class="c-gray en">&gt;</span> 采购管理 <span class="c-gray en">&gt;</span> 采购 <a class="btn btn-success radius r mr-20" style="line-height: 1.6em; margin-top: 3px" href="javascript:location.replace(location.href);" title="刷新"><i class="Hui-iconfont">&#xe68f;</i></a></nav>
    <div class="pd-20">
        <form runat="server">
            <table class="table table-hover table-bg table-sort">
                <tr>
                    <td><span>供应商：</span><asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
                    <span>仓库：</span><asp:DropDownList ID="DropDownList2" runat="server"></asp:DropDownList>
                   <span>订单类型：</span> <select id="span3">
                        <option value="0">采购</option>
                        <option value="1">退货</option>
                    </select>
                        </td>
                </tr>

            </table>

        </form>
    </div>
    <input type="text" class="input-text" style="width: 250px" placeholder="请输入商品编码" id="ids">
    <button type="submit" class="btn btn-success radius" onclick="fun()"><i class="Hui-iconfont">&#xe665;</i> 搜商品</button>
    <div class="mt-20">
        <table class="table table-border table-bordered table-hover table-bg table-sort">
            <thead>
                <tr class="text-c">
                    <th width="80">商品编号</th>
                    <th width="80">商品名称</th>
                    <th width="40">商品规格</th>
                    <th width="90">商品进价</th>
                    <th width="100">商品编码</th>
                    <th width="100">商品备注</th>
                    <th width="70">购买数量</th>
                    <th width="70">总金额</th>
                </tr>
            </thead>
            <tbody>
            </tbody>
        </table>
        <a class="btn btn-primary radius" onclick="Purchase_Add()" style="margin-left:70px; margin-top:30px;">采购商品</a>
    </div>
    <script type="text/javascript" src="lib/jquery/1.9.1/jquery.min.js"></script>
    <script type="text/javascript" src="lib/layer/1.9.3/layer.js"></script>
    <script type="text/javascript" src="lib/laypage/1.2/laypage.js"></script>
    <script type="text/javascript" src="lib/My97DatePicker/WdatePicker.js"></script>
    <script type="text/javascript" src="lib/datatables/1.10.0/jquery.dataTables.min.js"></script>
    <script type="text/javascript" src="js/H-ui.js"></script>
    <script type="text/javascript" src="js/H-ui.admin.js"></script>
    <script type="text/javascript">
        function fun() {
            var id = $("#ids").val();
            if ($("#num" + id + "").val() !=null)
            {
                alert("已添加此商品");
                return false;
            }
                
            $.ajax({
                type: "post",
                url: "Hander/GetProductByID.ashx",
                data: {
                    id: $("#ids").val()
                },
                success: function (data) {
                    var data1 = eval(data);
                    if (data1 == "") {
                        alert("无此商品");
                    }
                    else {
                        var str = "<tr class='text-c'><td><span name='Product_Id'>" + data1[0].Product_Id + "</span></td><td>" + data1[0].Product_Name + "</td><td>" + data1[0].Product_Modelspe + "</td><td><span name='Product_Bprid' id='Price" + data1[0].Product_Id + "' >" + data1[0].Product_Bprid + "</span>元</td><td>" + data1[0].Product_Barcode + "</td><td>" + data1[0].Product_Remark + "</td><td><input type='text' name='Sum' style='width:50px' onchange='Sum(" + data1[0].Product_Id + ")' id='num" + data1[0].Product_Id + "'/></td><td><span name='SumMoney' id='sum" + data1[0].Product_Id + "'>0</span>元</td></tr>";
                        $(".table-border tbody").append(str);
                    }
                }
            })
        
        }
        //小计
        function Sum(id) {
            var num = $("#num" + id + "").val();
            //正则验证（只能输入正整数）
            var tt = /^\d+$/g;    
            if (!tt.test(num)) {
                alert('必须是正整数');
                return false;
            }
            var price = $("#Price" + id + "").text();
            $("#sum" + id + "").text(price * num);
        }
        function Purchase_Add() {
            if ($("#DropDownList1").val() == "-请选择-" || $("#DropDownList2").val() == "-请选择-")
            {
                alert("请选择供货商和仓库");
                return false;
            }
            //获取商品编号
            var d = document.getElementsByName("Product_Id");
            var Product_Id = new Array();
            for (var i = 0; i < d.length; i++) {
                Product_Id[i] = d[i].innerHTML;
            }

            //获取商品单价
            var a = document.getElementsByName("Product_Bprid");
            var Product_Bprid = new Array();
            for (var i = 0; i < a.length; i++) {
                Product_Bprid[i] = a[i].innerHTML;
            }
            //获取各个商品的购买数量
            var Sum = new Array();
            var i = -1;
            $("[name='Sum']").each(function () {
                i++;
                Sum[i] = $(this).val();
            })
            //获去各个商品的商品小计
            var Money = 0;
            var c = document.getElementsByName("SumMoney");
            var SumMoney = new Array();
            for (var i = 0; i < c.length; i++) {
                SumMoney[i] = c[i].innerHTML;
                Money += parseFloat(c[i].innerHTML);
            }
            //字符拼接xml
            var xml = "<xml>";
            for (var i = 0; i < Sum.length; i++) {
                xml += "<product Product_Id='" + Product_Id[i] + "' PurchaseMate_Num='" + Sum[i] + "' PurchaseMate_Price='" + Product_Bprid[i] + "' SumMoney='" + SumMoney[i] + "'/>";
            }
            xml += "</xml>";
            var encodexml = encodeURIComponent(xml);// 对xml进行编码
           
            $.ajax({
                type: "post",
                url: "Hander/AddPurchase.ashx",
                data: {
                    Purchase_Type_State:$("#DropDownList3").val(),
                    Supplier_Id: $("#DropDownList1").val(),
                    WareHose_Id: $("#DropDownList2").val(),
                    Employee_Id: 1,
                    SumMoney:Money,
                    xml: encodexml
                },
                success: function (data) {
                    if (data > 0) {
                        alert("下单成功");
                    }
                },
                error:function(){
                    alert("服务器忙");
                }
            })
        }
       
    </script>

</body>
</html>

